package cn.fz.eventdemo01.utils;

import java.io.Serial;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;

public class XxResult extends LinkedHashMap<String, Object> implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;
    public static final int CODE_SUCCESS = 200;
    public static final int CODE_ERROR = 500;
    public static final int CODE_NOT_PERMISSION = 403;
    public static final int CODE_NOT_LOGIN = 401;

    public XxResult() {
    }

    public XxResult(int code, String msg, Object data) {
        this.setCode(code);
        this.setMsg(msg);
        this.setData(data);
    }

    public XxResult(Map<String, ?> map) {
        this.setMap(map);
    }

    public Integer getCode() {
        return (Integer)this.get("code");
    }

    public String getMsg() {
        return (String)this.get("msg");
    }

    public Object getData() {
        return this.get("data");
    }

    public void setCode(int code) {
        this.put("code", code);
    }

    public void setMsg(String msg) {
        this.put("msg", msg);
    }

    public void setData(Object data) {
        this.put("data", data);
    }

    public XxResult set(String key, Object data) {
        this.put(key, data);
        return this;
    }

    public <T> T get(String key, Class<T> cs) {
        return XxFoxUtil.getValueByType(this.get(key), cs);
    }

    public void setMap(Map<String, ?> map) {
        if (map != null) {

            for (String key : map.keySet()) {
                this.put(key, map.get(key));
            }
        }

    }

    public XxResult removeDefaultFields() {
        this.remove("code");
        this.remove("msg");
        this.remove("data");
        return this;
    }

    public XxResult removeNonDefaultFields() {

        for (String key : this.keySet()) {
            if (!"code".equals(key) && !"msg".equals(key) && !"data".equals(key)) {
                this.remove(key);
            }
        }

        return this;
    }

    public static XxResult ok() {
        return new XxResult(200, "ok", (Object)null);
    }

    public static XxResult ok(String msg) {
        return new XxResult(200, msg, (Object)null);
    }

    public static XxResult code(int code) {
        return new XxResult(code, (String)null, (Object)null);
    }

    public static XxResult data(Object data) {
        return new XxResult(200, "ok", data);
    }

    public static XxResult error() {
        return new XxResult(500, "error", (Object)null);
    }

    public static XxResult error(String msg) {
        return new XxResult(500, msg, (Object)null);
    }

    public static XxResult notLogin() {
        return new XxResult(401, "not login", (Object)null);
    }

    public static XxResult notPermission() {
        return new XxResult(403, "not permission", (Object)null);
    }

    public static XxResult get(int code, String msg, Object data) {
        return new XxResult(code, msg, data);
    }

    public static XxResult empty() {
        return new XxResult();
    }

    public String toString() {
        return "{\"code\": " + this.getCode() + ", \"msg\": " + this.transValue(this.getMsg()) + ", \"data\": " + this.transValue(this.getData()) + "}";
    }

    private String transValue(Object value) {
        if (value == null) {
            return null;
        } else {
            return value instanceof String ? "\"" + value + "\"" : String.valueOf(value);
        }
    }
}
